package main

import (
	"fmt"
	"math"
)

func main() {
	my := Constructor()
	var b bool
	b = my.Book(10, 20)
	fmt.Println(b)
	b = my.Book(50, 60)
	fmt.Println(b)
	b = my.Book(10, 40)
	fmt.Println(b)
	b = my.Book(5, 15)
	fmt.Println(b)
	b = my.Book(5, 10)
	fmt.Println(b)
	b = my.Book(25, 55)
	fmt.Println(b)
}

type MyCalendarTwo struct {
	pairs     []Pair
	foldPairs []Pair
}

type Pair struct {
	l int
	r int
}

func Constructor() MyCalendarTwo {
	return MyCalendarTwo{
		pairs:     make([]Pair, 0),
		foldPairs: make([]Pair, 0),
	}
}

func (this *MyCalendarTwo) Book(start int, end int) bool {
	pairs := this.pairs
	foldPairs := this.foldPairs
	for _, foldPair := range foldPairs {
		if start < foldPair.r && end > foldPair.l {
			return false
		}
	}

	for _, recPair := range pairs {
		if start < recPair.r && end > recPair.l {
			newFoldPair := Pair{
				l: int(math.Max(float64(start), float64(recPair.l))),
				r: int(math.Min(float64(end), float64(recPair.r))),
			}
			this.foldPairs = append(this.foldPairs, newFoldPair)
		}
	}
	newPair := Pair{
		l: start,
		r: end,
	}
	this.pairs = append(this.pairs, newPair)
	//fmt.Println(this.pairs, this.foldPairs)
	return true
}

/**
 * Your MyCalendarTwo object will be instantiated and called as such:
 * obj := Constructor();
 * param_1 := obj.Book(start,end);
 */
